package com.atguigu.yygh.hosp.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.util.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-01-12
 */
@Api(tags = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospital-set")
@Slf4j
//@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                             @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody  List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }

    @GetMapping("/detail")
    @ApiOperation("根据医院id查询医院详情")
    public R getHospitalSetDetail(@RequestParam("id") Integer id){
       return R.ok().data("item",hospitalSetService.getById(id));
    }

    @PostMapping("/updateHospSet")
    @ApiOperation("医院修改")
    public R getHospitalSetDetail(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    @ApiOperation(value = "添加医院信息")
    @PostMapping("/saveHospSet")
    public R save(@RequestBody HospitalSet hospitalSet){
        hospitalSet.setStatus(1);

        //签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));

        hospitalSetService.save(hospitalSet);
        return R.ok();
    }

    //@RequestBody就不要和@GetMapping一起使用
    @ApiOperation(value = "带查询条件的医院分页信息")
    @PostMapping("/{current}/{limit}")
    public R pageList(@PathVariable(value = "current") Integer current,
                      @PathVariable(value = "limit") Integer limit,
                     @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){

        Page page = new Page(current,limit);
        if(hospitalSetQueryVo == null){
            hospitalSetService.page(page);
        }else{
            String hosname = hospitalSetQueryVo.getHosname();
            String hoscode = hospitalSetQueryVo.getHoscode();
            QueryWrapper<HospitalSet> wrapper=new QueryWrapper<HospitalSet>();
            if(!StringUtils.isEmpty(hosname)){
                wrapper.like("hosname",hosname);
            }
            if(!StringUtils.isEmpty(hoscode)){
                wrapper.eq("hoscode",hoscode);
            }

            hospitalSetService.page(page,wrapper);
        }


        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());
    }

    @ApiOperation(value = "查询所有的医院信息")
    @GetMapping("/findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "根据医院的Id删除医院")
    @DeleteMapping("/remove/{id}")
    public R removeById(@ApiParam(name = "id",value = "医院ID",required = true) @PathVariable(value = "id" ) Integer id){
        boolean b = hospitalSetService.removeById(id);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }


}

